package cn.da.shuai.cool.ai.search.client.config;

import cn.da.shuai.cool.ai.search.client.store.MongodbMemoryStore;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore;
import io.milvus.common.clientenum.ConsistencyLevelEnum;
import io.milvus.param.IndexType;
import io.milvus.param.MetricType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AIConfig {

    @Autowired
    private EmbeddingModel embeddingModel;

    @Autowired
    private MongodbMemoryStore mongodbMemoryStore;

    @Bean
    public ChatMemoryProvider chatMemoryProvider() {
        return memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .chatMemoryStore(mongodbMemoryStore)
                .maxMessages(20)
                .build();
    }

//    @Bean
//    public EmbeddingStore<TextSegment> embeddingStore() {
//        List<Document> rags = ClassPathDocumentLoader.loadDocuments("rag");
////        List<Document> rags = ClassPathDocumentLoader.loadDocuments("rag", new ApachePdfBoxDocumentParser());
//        DocumentSplitter splitter = DocumentSplitters.recursive(500, 100, new HuggingFaceTokenizer());
//        InMemoryEmbeddingStore<TextSegment> store = new InMemoryEmbeddingStore<>();
//        EmbeddingStoreIngestor.builder()
//                .embeddingStore(store)
//                .documentSplitter(splitter)
//                .embeddingModel(embeddingModel)
//                .build()  
//                .ingest(rags);
//        return store;
//    }

    @Bean
    public EmbeddingStore<TextSegment> embeddingStore() {
        return MilvusEmbeddingStore.builder()
                .host("127.0.0.1")                         // Host for Milvus instance
                .port(19530)                               // Port for Milvus instance
                .collectionName("cool")                    // Name of the collection
                .dimension(embeddingModel.dimension())     // Dimension of vectors
                .indexType(IndexType.FLAT)                 // Index type
                .metricType(MetricType.COSINE)             // Metric type
//                .username("username")                      // Username for Milvus
//                .password("password")                      // Password for Milvus
                .consistencyLevel(ConsistencyLevelEnum.EVENTUALLY)  // Consistency level
                .autoFlushOnInsert(false)                   // Auto flush after insert
                .idFieldName("id")                         // ID field name
                .textFieldName("text")                     // Text field name
                .metadataFieldName("metadata")             // Metadata field name
                .vectorFieldName("vector")                 // Vector field name
                .build();
    }

    @Bean
    public ContentRetriever contentRetriever(EmbeddingStore<TextSegment> embeddingStore) {
        return EmbeddingStoreContentRetriever.builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(embeddingModel)
                .minScore(0.8)
                .maxResults(1)
                .build();
    }
}
